import { headerActionTypes } from './index'
import { fromJS } from 'immutable'

// 将对象转换为immutable对象，immutable意为不可修改
const defaultState = fromJS({
  mouseIn: false,
  focus: false,
  list: [],
  page: 1,
  totalPage: 1
})
const reducer = (state = defaultState, action) => {
  switch (action.type) {
    case headerActionTypes.CHANGE_INPUT_STATUS:
      // immutable对象的set方法会结合之前的对象的值返回一个新的对象
      return state.set('focus', action.value)
    case headerActionTypes.CHANGE_LIST:
      return state.merge({
        list: action.value,
        totalPage: action.totalPage
      })
    case headerActionTypes.MOUSE_ENTER:
      return state.set('mouseIn', true)
    case headerActionTypes.MOUSE_LEAVE:
      return state.set('mouseIn', false)
    case headerActionTypes.CHANGE_PAGE:
      let { page, totalPage } = action
      return state.set('page', page >= totalPage ? 1 : page++)
    default:
      return state
  }
}

export default reducer